layui.define(function(exports){
  


    layui.use(['table', 'form', 'layer','setter','view','admin','laytpl','laypage'],function(){
        var $ = layui.$,
        admin = layui.admin,
        laytpl = layui.laytpl
        laypage = layui.laypage
        view = layui.view,
        table = layui.table,
        form = layui.form,
        layer = layui.layer,
        router = layui.router(),
        setter = layui.setter;
        pageindex = 1;
        pagesize = 8

    // 获取产品数据
    var getDate = function(type,pageindex,pagesize,productName,productCode){
        admin.req({
            url:setter.url + '/product/selectByProduct',
            type:'post',
            data:{
                pageindex,
                pagesize,
                productName,
                productCode
            },
            done:function(res){
                if(res.code === '200'){
                    var scriptHtml = document.getElementById('item-list').innerHTML;
                    var item = document.getElementById('item')
                    laytpl(scriptHtml).render(res.rows,function(html){
                        item.innerHTML = html;
                    })
                    if(type !== 'page'){
                        pageRender(res.count)
                    }
                    if(res.count == 0){
                        layer.msg('未查询到结果')
                    }
                    
                }
            }
        })

        
    }

    getDate('load',pageindex,pagesize,null,null);

    // 分页功能
    var pageRender = function(count){
        laypage.render({
        elem:'page',
        count:count,
        limit:10,
        limits:[10,15,20,25,30],
        layout:['count','prev','page','next','limit','skip'],
        jump:function(obj,first){
            // obj.curr  index
            // obj.limit size
            if(!first){
                getDate('page',obj.curr,obj.limit,null,null)
            }
        }
        })
    }   

     // 添加产品功能
     $('.add-btn').on('click',function(){
        admin.popup({
            title:'添加产品',
            area:['500px','500px'],
            id:'LAY_product_popup_create',
            success:function(leyero,index){
                view(this.id).render('equipment/productPopup-create',{type:'create'})
                .done(function(){
                    form.render(null,'layui-product-popup-list')
                })
            }
        })
    })

    // 搜索功能
    form.on('submit(Lay-product-serach)',function(obj){
        var field = obj.field;
        getDate('search',pageindex,pagesize,field.productName,field.productCode)
    })


    });

    
    


  
  exports('product', {});
});  